#
# 从惠农网(https://www.cnhnb.com/)获取价格信息
# 该网站有防护措施，仅获取第一页数据（15条）
#

import requests
import re
import math
import random
import string
import base64
import hashlib
import time
from bs4 import BeautifulSoup
import json
import CapturerDb

def waitSeconds(second):
    time.sleep(second)

def generateRandomString(length):
    # 字符集包括小写字母、大写字母和数字
    letters = string.ascii_letters + string.digits
    return ''.join(random.choice(letters) for _ in range(length))

def generateRandomCookies():
    timestamp = str(int(time.time()))
    md5Hash = hashlib.md5()
    md5Hash.update(timestamp.encode('utf-8'))
    md5=md5Hash.hexdigest()
    signtrue = ('%s,%s' % (timestamp, md5))
    return str(base64.b64encode(signtrue.encode('utf-8')), 'utf-8')


#根据分类码、品类代码获取价格，代码由源网站提供
def getPrices(pid, variety, priceName):
    print('开始全国农产品批发市场价格信息网' + priceName + '数据...')
    
    waitSeconds(3) #等待3秒

    #Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3
    #Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36
    #Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:126.0) Gecko/20100101 Firefox/126.0
    
    headers = {
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36',
        'Host':	'www.cnhnb.com',
        'Referer':'https://www.cnhnb.com/'
    }
    cookies = {'wdcid': generateRandomCookies()}
    url = f'https://pfsc.agri.cn/api/priceQuotationController/pageList?key=&order='
    
    queryParam = {"pageNum":1,"pageSize":999,"marketId":"","provinceCode":"","pid":pid,"varietyId":variety}
    response = requests.post(url, verify=False, headers=headers, cookies=cookies, json=queryParam)
    if response.status_code == 200:
        #print(response.text)
        try:
            data = json.loads(response.text)
            if data["code"]== 200:
                for item in data["content"]["list"]:
                    url = 'https://pfsc.agri.cn/#/priceMarket'
                    timeText = item["inStorageTime"]
                    varietyText = priceName
                    placeText = item["provinceName"]
                    priceText = item["middlePrice"]
                    priceUnit = item["meteringUnit"]
                    #print(priceName, varietyText, timeText, '全国农产品批发市场价格信息网', placeText, priceText, priceUnit)

                    CapturerDb.updatePrice('全国农产品批发市场价格信息网', '价格指数', url, placeText, timeText, priceName, varietyText, priceText, priceUnit)
        except Exception as e:
            print('获取全国农产品批发市场价格信息网' + priceName + '信息' + f"发生了异常: {e}")
    
        print('获取全国农产品批发市场价格信息网' + priceName + '信息完成!')
    else:
        print('获取全国农产品批发市场价格信息网' + priceName + '信息失败!')

#测试
#getPrices('AA', "9", '玉米')
#getPrices('AA', "20", '大豆')
#getPrices('AA', "1", '小麦')
#getPrices('AA', "1444", '水稻')
#getPrices('AA', "1445", '水稻')
#getPrices('AB', "32", '油菜籽')